package com.sz.biz.common.utils;

import com.sz.biz.pay.constants.PayConstant;
import com.sz.common.pay.common.DateGenerator;

import java.util.Random;

/**
 * Function: NOGenerator <br>
 * Author: siqishangshu <br>
 * Date: 2017-10-31 10:40:00
 */
public class NOGenerator {

    private static int SEQUENCE_NUMBER            = 0;
    private static int SEQUENCE_MAX               = 10000;
    private static String ACCOUNT_NO_PREFIX       = "SZ";
//    private static String ZERO_PREFIX             = "00";
    private static String TRAN_NO_PREFIX          = "TR0";

    /**
     *  2+2+3+6+5+5 = 23
     *  SZ 03 TRA 171103 00004 06006
     * @param accountType
     * @return
     */
    public synchronized static String getAccountNo(int accountType){
        switch (accountType){
            case PayConstant.AccountType.SUMMARY:
                return getGeneratorNo(accountType,"SUM");
            case PayConstant.AccountType.LOGISTICS:
                return getGeneratorNo(accountType,"LOG");
            case PayConstant.AccountType.TRADE:
                return getGeneratorNo(accountType,"TRA");
            case PayConstant.AccountType.BOTH:
                return getGeneratorNo(accountType,"BOT");
            case PayConstant.AccountType.PLATFORM:
                return getGeneratorNo(accountType,"PLA");
            case PayConstant.AccountType.SUPPLIER:
                return getGeneratorNo(accountType,"SUP");
            case PayConstant.AccountType.OTHER:
                return getGeneratorNo(accountType,"OTH");
            default:
                return getGeneratorNo(accountType,"OTH");
        }
    }

    /**
     *  2+2+3+10+5+5 = 29
     *  SZ 03 TRA 20171103 120000 00004 06006
     * @param transacationType
     * @return
     */
    public synchronized static String getTranNo(int transacationType){
        switch (transacationType){
            case PayConstant.TranscationType.RECHARGE:
                return getGeneratorTranNo(transacationType,"CHA");
            case PayConstant.TranscationType.WITHDRAWAL:
                return getGeneratorTranNo(transacationType,"DRA");
            case PayConstant.TranscationType.PAY:
                return getGeneratorTranNo(transacationType,"PAY");
            case PayConstant.TranscationType.OTHER:
                return getGeneratorTranNo(transacationType,"OTH");
            default:
                return getGeneratorTranNo(transacationType,"OTH");
        }
    }

    private static String getGeneratorTranNo(int transacationType, String string) {
        return TRAN_NO_PREFIX+ transacationType+string+ DateGenerator.getyyyyMMddHHmmss()+getSequenceNumber()+getRandom();
    }

    private static String getGeneratorNo(int accountType,String string) {
        return ACCOUNT_NO_PREFIX+ String.format("%03d",accountType)+string+ DateGenerator.getyyMMdd()+getSequenceNumber()+getRandom();
    }

    private static String getSequenceNumber(){
        SEQUENCE_NUMBER = (++SEQUENCE_NUMBER) % SEQUENCE_MAX;
        return String.format("%05d",SEQUENCE_NUMBER);
    }

    private static String getRandom(){
        Random random = new Random();
        return String.format("%05d", random.nextInt(9999));
    }


}
